let productId = sessionStorage.getItem("ReplayId");
$(function () {
    //跟据id查询用户信息，主要是为了取得email
    $.ajax({
        url: "http://localhost:8080/back/message/findById",
        data: {"id": productId},
        dataType: "json",
        headers:{'username':localStorage.getItem("loginUser")},
        success: function (res) {
            // console.log("跟据id查询到的反馈信息",res.data);
            sendMail(res.data)
        }
    })
})


//向用户的邮箱发送回复信息
function sendMail(res) {

    layui.use(['upload', 'element', 'layer', 'layedit', 'form'], function () {
        var $ = layui.jquery
            , layer = layui.layer
            , form = layui.form
            , layedit = layui.layedit;


        //一定要渲染一次,让数据填充成功
        form.render("radio");
        layedit.set({
            uploadImage: {
                url: 'http://localhost:8080/back/product/upload' //接口url
            }
        });
        //建立编辑器
        var build = layedit.build('productDesc');
        //监听修改提交
        //提交编辑内容
        form.on('submit(edit)',
            function (data) {
                data.field.subject = res.subject;
                data.field.email = res.email;
                data.field.feedback = layedit.getContent(build);
                console.log("提交的内容", data.field);

                $.ajax({
                    url: "http://localhost:8080/back/message/sendMail",
                    type: "post",
                    headers:{'username':localStorage.getItem("loginUser")},
                    data: JSON.stringify(data.field),
                    contentType: "application/json",
                    dataType: "json",
                    success: function (res) {

                        let user  = {"id":productId};
                        if (res.msg == "success") {
                            /*回复成功更改回复状态*/
                            $.ajax({
                                url: "http://localhost:8080/back/message/updateById",
                                data: JSON.stringify(user),
                                headers:{'username':localStorage.getItem("loginUser")},
                                contentType: "application/json",
                                dataType: "json",
                                type: "post",
                                success: function () {
                                    //发异步，把数据提交给php
                                    layer.alert("回复成功", {
                                            icon: 6
                                        },
                                        function () {
                                            //关闭当前frame
                                            xadmin.close();

                                            // 可以对父窗口进行刷新
                                            xadmin.father_reload();
                                        })
                                }
                            })
                        } else {
                            layer.alert("回复失败");
                        }
                    }
                })
                return false;
            });

    });
}